package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.adapter.guang.BatteryImageAdapter
import com.c.ctgproject.adapter.guang.BatteryImageV2Adapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryCanHandlerDetailBinding
import com.c.ctgproject.entity.IfUpdate
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.PowerBatteryEntity
import com.c.ctgproject.entity.guang.PowerBatteryEntityV2
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CarInfoLayout
import com.c.ctgproject.wiget.dialog.ShowImgVideoFragment
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/10
 *描述: 可拆电池详情 页面 电池详情 页面
 */
class BatteryCanHandlerDetailActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryCanHandlerDetailBinding.inflate(layoutInflater) }
    private var data:PowerBatteryEntityV2.PowerBatteryBean?=null
    private var pbid=-1
    override fun getLayout() = mBinding.root

    override fun event() {
         pbid = intent.getIntExtra(MyConstants.ID, -1)



//        val dataStr = intent.getStringExtra(MyConstants.DATA).noDataHandler()
//        if (dataStr.isNotEmpty()) {
//            val data = getGson().fromJson(dataStr, BatteryCanHandlerEntity.DataBean.ListBean::class.java)
//
//            mBinding.mContainer1.addView(CarInfoLayout(this, "入库时间", data.createTime.noDataHandler()))
//            val stockStatus = when (data.stockStatus) {
//                1 -> "在库"
//                2 -> "已售"
//                else -> {
//                    "暂无数据"
//                }
//            }
//            mBinding.mContainer1.addView(CarInfoLayout(this, "库存状态", stockStatus))
//            mBinding.mContainer1.addView(CarInfoLayout(this, "电池名称", data.batteryName.noDataHandler()))
//            mBinding.mContainer1.addView(CarInfoLayout(this, "电池自编码", data.batteryCode.noDataHandler()))
//            mBinding.mContainer1.addView(CarInfoLayout(this, "电池包自编码", data.batteryPackCode.noDataHandler()))
//            mBinding.mContainer1.addView(CarInfoLayout(this, "模组编码", data.moduleCode.noDataHandler()))
//            val enterMode = when (data.enterMode) {
//                1 -> "电池包"
//                2 -> "模组"
//                3 -> "电芯"
//                else -> {
//                    "暂无数据"
//                }
//            }
//            mBinding.mContainer1.addView(CarInfoLayout(this, "入库方式", enterMode))
//
//            val batterySource = when (data.batterySource) {
//                1 -> "拆解"
//                2 -> "散件"
//                else -> {
//                    "暂无数据"
//                }
//            }
//            mBinding.mContainer1.addView(CarInfoLayout(this, "电池来源", batterySource))
//
//
//            mBinding.mContainer2.addView(CarInfoLayout(this, "数量", data.count.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "重量KG", data.weight.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "容量Ah", data.capacityAh.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "电压V", data.voltage.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "模组数量", data.moduleNum.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "容量KWh", data.capacityKWh.noDataHandler()))
//            mBinding.mContainer2.addView(CarInfoLayout(this, "备注", data.remark.noDataHandler()))
//
//
//        }

    }

    override fun onResume() {
        super.onResume()
        ApiMethods.apiSubscribe(ApiMethods.retrofit.ifUpdate(),MyObserverHandlerLoading(this,loading){
            MyLog.d("是否可以编辑回调:$it")
            val result= getGson().fromJson(it,IfUpdate::class.java).ifUpdate
            if(result==1){
                mBinding.mBottomRL.visibility=View.VISIBLE
            }
        })
        ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatteryDetail(pbid), MyObserverHandlerLoading(this, loading) {
            MyLog.d("动力电池详情回调:$it")
            val result = getGson().fromJson(it, PowerBatteryEntityV2::class.java).powerBattery
            mBinding.mContainer1.removeAllViews()
            mBinding.mContainer2.removeAllViews()
            result?.let { data ->
                this.data=data
                //后台控制是否显示编辑按钮
                mBinding.mContainer1.addView(
                    CarInfoLayout(this, "电池状态",  when (data.status) {
                        0 -> "待入库"
                        1 -> "已入库"
                        3 -> "已出库"
                        else -> ""
                    })
                )

//                //改成两种状态
//                mBinding.mContainer1.addView(CarInfoLayout(this, "电池状态", when(data.returnStatus){1->"已退库"
//                    else -> {"已入库"}
//                }))
                //0-待入库 1-已入库 3-已出库
//                mBinding.mContainer1.addView(CarInfoLayout(this, "电池状态", when(data.status){0->{if(data.returnStatus==1)"已退库" else "待入库"} 1->"已入库" 2->"已出库"
//                    else -> {""}
//                }))
                //是否拆解 0-未拆解 1-拆解中 2-已拆解
                mBinding.mContainer1.addView(CarInfoLayout(this, "是否拆解", when(data.dismantle){0->"未拆解" 1->"拆解中" 2->"已拆解"
                    else -> {""}
                }))

                mBinding.mContainer1.addView(CarInfoLayout(this, "车辆自编码", data.carCode.noDataHandler()))
                //是否拆解 0-未拆解 1-拆解中 2-已拆解
                mBinding.mContainer1.addView(CarInfoLayout(this, "入库人", data.createPersonName.noDataHandler()))
                mBinding.mContainer1.addView(CarInfoLayout(this, "入库时间", data.createTime.noDataHandler()))

                //牛了个大逼的嘛
//                mBinding.mContainer1.addView(CarInfoLayout(this, "磅单号", data.poundNo.noDataHandler()))
                val status = when (data.status) {
                    0 -> "待入库"
                    1 -> "已入库"
                    2 -> "已拆解"
                    3 -> "已出库"
                    4 -> "拆解中"
                    else -> {
                        "暂无数据"
                    }
                }

                //mBinding.mContainer1.addView(CarInfoLayout(this, "电池状态", status))

                mBinding.mContainer1.addView(CarInfoLayout(this, "电池名称", data.batteryName.noDataHandler()))
                mBinding.mContainer1.addView(CarInfoLayout(this, "电池编码", data.batteryCode.noDataHandler()))
                mBinding.mContainer1.addView(CarInfoLayout(this, "电池自编码", data.batterySelfCode.noDataHandler()))
                mBinding.mContainer1.addView(CarInfoLayout(this, "入库编码", data.bulkInOrderNo.noDataHandler()))
//                mBinding.mContainer1.addView(CarInfoLayout(this, "电池包自编码", data.batteryPackCode.noDataHandler()))
//                mBinding.mContainer1.addView(CarInfoLayout(this, "模组编码", data.moduleCode.noDataHandler()))
                val enterMode = when (data.enterMode) {
                    1 -> "整车电池"
                    2 -> "散件"
                    3 -> "电池拆解"
                    else -> "暂无数据"
                }
                mBinding.mContainer1.addView(CarInfoLayout(this, "入库方式", enterMode))

                val batteryType = when (data.batteryType) {
                    1 -> "电池包"
                    2 -> "模组"
                    3 -> "电芯"
                    else -> "暂无数据"
                }
                mBinding.mContainer1.addView(CarInfoLayout(this, "电池类型", batteryType))

//                val batterySource = when (data.batterySource) {
//                    1 -> "拆解"
//                    2 -> "散件"
//                    else -> {
//                        "暂无数据"
//                    }
//                }
//                mBinding.mContainer1.addView(CarInfoLayout(this, "电池来源", batterySource))


                mBinding.mContainer2.addView(CarInfoLayout(this, "数量", data.count.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "重量KG", data.weight.noDataHandler()))
                //不要展示暂无数据了
                mBinding.mContainer2.addView(CarInfoLayout(this, "模组数量", data.moduleCount.handlerNull()))
//                mBinding.mContainer2.addView(CarInfoLayout(this, "重量KG", data.weight.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "容量Ah", data.capacityAh.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "电压V", data.voltage.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "容量KWh", data.capacityKwh.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "库位", data.stockPosi.noDataHandler()))
                mBinding.mContainer2.addView(CarInfoLayout(this, "备注", data.remark.noDataHandler()))
                //所有图片
                data.carPictures?.let { list ->
                    mBinding.mImageRV.adapter = BatteryImageV2Adapter(this, list)
                    mBinding.mImageRV.layoutManager = LinearLayoutManager(this)
                }
            }
        })
    }

    override fun click() {
        mBinding.mEditBTN.setOnClickListener {
            if(data!=null){
                startActivity<EditBatteryCanHandlerActivity>(MyConstants.DATA to data)
            }
        }
    }
}